namespace TSL.DAL
{
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Text;
    using TSL.Model;

    public class dict
    {
        public void Adddict(TSL.Model.dict model)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_dict_Add");
            db.AddInParameter(dbCommand, "id", DbType.AnsiString, model.id);
            db.AddInParameter(dbCommand, "ClassName", DbType.AnsiString, model.ClassName);
            db.AddInParameter(dbCommand, "textName", DbType.AnsiString, model.textName);
            db.AddInParameter(dbCommand, "ClassID", DbType.AnsiString, model.ClassID);
            db.ExecuteNonQuery(dbCommand);
        }

        public void Deletedict(string ID)
        {
            List<string> strList = new List<string>();
            strList.Add("delete from dict where ID = '" + ID + "'");
            DbHelperSQL.ExecuteSqlTran(strList);
        }

        public bool Exists(string name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from dict");
            strSql.Append(" where sscname=@name ");
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@name", SqlDbType.VarChar, 50) };
            parameters[0].Value = name;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public List<TSL.Model.dict> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from V_dict ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append("order by id asc");
            List<TSL.Model.dict> list = new List<TSL.Model.dict>();
            using (IDataReader dataReader = DatabaseFactory.CreateDatabase().ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(this.ReaderBind(dataReader));
                }
            }
            return list;
        }

        public DataSet GetListFromView(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from V_dict order by orderid asc");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DatabaseFactory.CreateDatabase().ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public TSL.Model.dict GetModel(string ID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_dict_GetModelByID");
            db.AddInParameter(dbCommand, "ID", DbType.AnsiString, ID);
            TSL.Model.dict model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = this.ReaderBind(dataReader);
                }
            }
            return model;
        }

        public TSL.Model.dict GetModelByClassID(string ID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_dict_GetModelByClassID");
            db.AddInParameter(dbCommand, "ID", DbType.AnsiString, ID);
            TSL.Model.dict model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = this.ReaderBind(dataReader);
                }
            }
            return model;
        }


        public TSL.Model.dict GetModels(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from V_dict ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append("order by id asc");
            List<TSL.Model.dict> list = new List<TSL.Model.dict>();
            Database db = DatabaseFactory.CreateDatabase();
            TSL.Model.dict model = null;
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                if (dataReader.Read())
                {
                    model = this.ReaderBind(dataReader);
                }
            }
            return model;
        }

        public TSL.Model.dict ReaderBind(IDataReader dataReader)
        {
            TSL.Model.dict model = new TSL.Model.dict();
            model.id = dataReader["id"].ToString();
            model.ClassName = dataReader["ClassName"].ToString();
            model.textName = dataReader["textName"].ToString();
            model.ClassID = dataReader["ClassID"].ToString();
            return model;
        }

        public void Update(TSL.Model.dict model)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_dict_Update");
            db.AddInParameter(dbCommand, "id", DbType.AnsiString, model.id);
            db.AddInParameter(dbCommand, "ClassName", DbType.AnsiString, model.ClassName);
            db.AddInParameter(dbCommand, "textName", DbType.AnsiString, model.textName);
            db.AddInParameter(dbCommand, "ClassID", DbType.AnsiString, model.ClassID);
            db.ExecuteNonQuery(dbCommand);
        }
    }
}

